Page({
  data: {
    channelInfo: {
      coverUrl: 'https://cloud-minapp-48535.cloud.ifanrusercontent.com/1uIfUlcixEqQmCgO.jpg',
      title: '木木彩绘容妆0基础，100节课程，10节临摹板+90节课程',
      description: '已更新121节/共121节',
    },
    groups: []
  },

  onLoad: function (options) {
    // 这里可以获取频道ID，然后请求数据
    const channelId = options.id || '1474484';
    this.loadChannelData(channelId);
  },

  loadChannelData: function (channelId) {
    let that = this
    wx.showLoading({ title: '加载中...' });
    wx.request({
      url: 'https://cloud-minapp-48535.cloud.ifanrusercontent.com/1uIfKaZDqa5wjZef.json',
      success: function(res) {
        try {
          const jsonData = res.data // 自动解析JSON
          that.setData({
            groups : jsonData["groups"]
          })
          console.log('获取的JSON数据：', jsonData)
          wx.hideLoading()
        } catch (e) {
          console.error('JSON解析错误：', e)
          wx.hideLoading()
        }
      }
    })
  },

  toggleGroup: function (e) {
    const index = e.currentTarget.dataset.index;
    const key = `groups[${index}].isExpanded`;
    this.setData({
      [key]: !this.data.groups[index].isExpanded
    });
  },

  // 跳转到课程详情
  navigateToCourse: function (e) {
    const item = e.currentTarget.dataset.item;
    console.log(item)
    wx.navigateTo({
      url: `/pages/draw/play?id=${item.id}&title=${item.title}&url=${item.videoUrl}`
    });
  },

  onPullDownRefresh: function () {
    // 下拉刷新
    this.loadChannelData();
    wx.stopPullDownRefresh();
  }
});